package com.colin.library.help;

import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.colin.library.base.R;
import com.colin.library.listener.IWebViewCallBack;

import static android.view.View.GONE;


/**
 * Created by ColinLu on 2016/8/5.
 */
public class InitViewHelp {
    private static volatile InitViewHelp instance = null;

    private InitViewHelp() {
    }

    public static InitViewHelp getInstance() {
        if (instance == null) {
            synchronized (InitViewHelp.class) {
                if (instance == null) {
                    instance = new InitViewHelp();
                }
            }
        }
        return instance;
    }

    /**
     * @param swipeRefreshLayout
     */
    @SuppressWarnings("ResourceAsColor")
    public void initSwipeRefreshLayout(@NonNull SwipeRefreshLayout swipeRefreshLayout) {
        swipeRefreshLayout.setProgressBackgroundColorSchemeColor(swipeRefreshLayout.getContext().getResources().getColor(R.color.colorPrimary));

        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
    }


    public void initWebView(WebView webview, String url, IWebViewCallBack iWebViewCallBack) {
        // 设置支持JavaScript脚本
        WebSettings webSettings = webview.getSettings();
        webSettings.setDefaultTextEncodingName("UTF-8");
        webSettings.setJavaScriptEnabled(true);// 设置可以运行JS脚本
        // 设置可以访问文件
        webSettings.setAllowFileAccess(true);
        // 设置可以支持缩放
        webSettings.setSupportZoom(false);
        // 设置默认缩放方式尺寸是far
        webSettings.setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);
        // 设置出现缩放工具
        webSettings.setBuiltInZoomControls(false);
        webSettings.setDefaultFontSize(20);
        // 允许js弹出窗口
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setUseWideViewPort(true);// 打开页面时， 自适应屏幕
        webSettings.setLoadWithOverviewMode(true);// 打开页面时， 自适应屏幕
        // webSettings.setTextZoom(120);//Sets the text zoom of the page in
        // percent. The default is 100.
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webview.setBackgroundResource(R.color.transparent);
//        webview_framgnet_mall.loadUrl(url);// 加载网址
        // 添加js交互接口类，并起别名 imagelistner
        webview.setWebViewClient(new MyWebViewClient(iWebViewCallBack));
        webview.setWebChromeClient(new MyWebChromeClient(iWebViewCallBack));
        webview.loadUrl(url);
    }


    private class MyWebViewClient extends WebViewClient {
        private IWebViewCallBack iWebViewCallBack;

        public MyWebViewClient(IWebViewCallBack iWebViewCallBack) {
            this.iWebViewCallBack = iWebViewCallBack;
        }



        // url拦截
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (null!=iWebViewCallBack){
                iWebViewCallBack.loadUrl(url);
            }
            // 使用自己的WebView组件来响应Url加载事件，而不是使用默认浏览器器加载页面
            view.loadUrl(url);
            // 相应完成返回true
            return true;
        }

        // 页面开始加载
        @Override
        public void onPageStarted(WebView webView, String url, Bitmap favicon) {
            if (null!=iWebViewCallBack){
                iWebViewCallBack.loadProgress(View.VISIBLE,0);
            }
            webView.getSettings().setJavaScriptEnabled(true);
            super.onPageStarted(webView, url, favicon);
        }

        // 页面加载完成
        @Override
        public void onPageFinished(WebView webView, String url) {
            webView.setVisibility(View.VISIBLE);
            webView.invalidate();
            super.onPageFinished(webView, url);

            if (null!=iWebViewCallBack){
                iWebViewCallBack.loadProgress(GONE,100);
            }
        }

        // WebView加载的所有资源url
        @Override
        public void onLoadResource(WebView view, String url) {
            super.onLoadResource(view, url);
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            if (null!=iWebViewCallBack){
                iWebViewCallBack.loadProgress(View.GONE,100);
                iWebViewCallBack.error(errorCode,description,failingUrl);
            }
            super.onReceivedError(view, errorCode, description, failingUrl);
        }
    }

    private class MyWebChromeClient extends WebChromeClient {
        private IWebViewCallBack iWebViewCallBack;

        public MyWebChromeClient(IWebViewCallBack iWebViewCallBack) {

        }

        @Override
        // 处理javascript中的alert
        public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
            return super.onJsAlert(view, url, message, result);
        }

        @Override
        // 处理javascript中的confirm
        public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
            return super.onJsConfirm(view, url, message, result);
        }


        @Override
        // 处理javascript中的prompt
        public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, final JsPromptResult result) {
            return super.onJsPrompt(view, url, message, defaultValue, result);
        }


        // 设置网页加载的进度条
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (null!=iWebViewCallBack){
                iWebViewCallBack.loadProgress(View.VISIBLE,newProgress);
            }
            super.onProgressChanged(view, newProgress);
        }

        // 设置程序的Title
        @Override
        public void onReceivedTitle(WebView view, String title) {
            if (null!=iWebViewCallBack){
                iWebViewCallBack.setTitle(title);
            }
            super.onReceivedTitle(view, title);
        }
    }
}
